package com.github.shiqiyue.learn.guava;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/***
 * 描述
 * @author wenyao.wu
 * @date 2018/7/4
 */
public class MultiMapTest {

    private static final Logger logger = LoggerFactory.getLogger(MultiMapTest.class);


    public static void main(String[] args) {
        /***
         * 初始化一个multimap,
         * multimap格式，一个key可以对应多个value
         */
        Multimap<String, String> multimap = ArrayListMultimap.create();

        multimap.put("lower", "a");
        multimap.put("lower", "b");
        multimap.put("lower", "c");
        multimap.put("lower", "d");
        multimap.put("lower", "e");
        multimap.put("lower", "e");

        multimap.put("upper", "A");
        multimap.put("upper", "B");
        multimap.put("upper", "C");
        multimap.put("upper", "D");
        List<String> lowerList = (List<String>) multimap.get("lower");
        logger.info("Initial lower case list");
        logger.info(lowerList.toString());
        lowerList.add("f");
        logger.info("Modified lower case list");
        logger.info(lowerList.toString());

        List<String> upperList = (List<String>) multimap.get("upper");
        logger.info("Initial upper case list");
        logger.info(upperList.toString());
        upperList.remove("D");
        logger.info("Modified upper case list");
        logger.info(upperList.toString());

        Map<String, Collection<String>> map = multimap.asMap();
        logger.info("Multimap as a map");
        for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
            String key = entry.getKey();
            Collection<String> value = multimap.get("lower");
            logger.info(key + ":" + value);
        }

        logger.info("Keys of Multimap");
        Set<String> keys = multimap.keySet();
        for (String key : keys) {
            logger.info(key);
        }

        logger.info("Values of Multimap");
        Collection<String> values = multimap.values();
        logger.info("{}", values);

    }
}
